<?php 

namespace pmvc\aop;

class Proxy {

	private $proxiedClass			= NULL;
	private $object 				= NULL;
	private $methodInterceptor		= NULL;
	private $propertyInterceptor	= NULL;
	private $userData				= NULL;

	public function __construct(
		$object,
		MethodInterceptor $methodInterceptor,
		PropertyInterceptor $propertyInterceptor=NULL,
		$userData=NULL) {
		
		$this->object				= $object;
		$this->proxiedClass			= get_class($object);
		$this->methodInterceptor	= $methodInterceptor;
		$this->propertyInterceptor	= $propertyInterceptor;
		$this->userData				= $userData;
	}

	public function getProxiedClass() {
		return $this->proxiedClass;
	}

	public function getProxiedObject() {
		return $this->object;
	}
	
	public function __call($methodName, $args) {
		if (pmvc_hasVal($this->methodInterceptor)) {
			return $this->methodInterceptor->methodInvoked(
				$this->object, $methodName, $args, $this->userData);
		} else {
			call_user_func_array(array($methodName, $this->object), $args);
		}
	}

	public function __set($name, $value) {
		if (pmvc_hasVal($this->propertyInterceptor)) {
			$this->propertyInterceptor->set(
				$this->object, $name, $value, $this->userData);
		} else {
			$this->object->$name = $value;
		}
	}

	public function __get($name) {
		if (pmvc_hasVal($this->propertyInterceptor)) {
			return $this->propertyInterceptor->get(
				$this->object, $name, $this->userData);
		} else {
			return $this->object->$name;
		}
	}
	
	public function __isset($name) {
		if (pmvc_hasVal($this->propertyInterceptor)) {
			return $this->propertyInterceptor->is_set(
				$this->object, $name, $this->userData);
		} else {
			return isset($this->object->$name);
		}
	}
	
	public function __unset($name) {
		if (pmvc_hasVal($this->propertyInterceptor)) {
			$this->propertyInterceptor->un_set(
				$this->object, $name, $this->userData);
		} else {
			unset($this->object->$name);
		}
	}
}

?>